﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ue.Framework.Web;
using Ue.Infrastructure.Web.AsyncHandlers;
namespace Ue.Qcdz.UI.Controller
{
    public class ArticleController : AsyncResult
    {
        public string tagKey = string.Empty;
        public string nodeKey = string.Empty;
        public ArticleController(AsyncCallback callback, HttpContext context, object state)
            : base(callback, context, state)
        {
            //不需要缓存
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            context.Response.Expires = 0; context.Response.CacheControl = "no-cache";
            //获取参数
            nodeKey = context.Request.GetQueryValue<string>("NodeId");
            tagKey = context.Request.GetQueryValue<string>("TagId");
        }

        public string GetTag()
        {
            if (!string.IsNullOrEmpty(tagKey))
            {
                Ue.Qcdz.Domain.Tags.Tag model = Ue.Qcdz.Domain.Tags.TagService.GetTag(tagKey);
                return ObjectToJson(model);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取节点列表
        /// </summary>
        /// <returns></returns>
        public string GetChildNodes()
        {
            IList<Ue.Qcdz.Domain.TreeViews.TreeNode> list = null;
            Ue.Qcdz.Domain.TreeViews.TreeNode node;

            if (string.IsNullOrEmpty(nodeKey))
            {
                list = Ue.Qcdz.Domain.Articles.ArticleService.TagTreeView.ChildNodes.ToList();
            }
            else
            {
                node = Ue.Qcdz.Domain.Articles.ArticleService.TagTreeView.FindNodeBy(new Guid(nodeKey));
                if (node != null)
                    list = node.ChildNodes.ToList();
            }

            return ObjectToJson(list);
        }


        #region ：私有的方法

        private string ObjectToJson(Ue.Qcdz.Domain.Tags.Tag model)
        {
            if (model == null) return string.Empty;
            StringBuilder json = new StringBuilder();
            json.Append("[{");
            json.Append("\"Name\":\"" + model.Name + "\",");
            json.Append("\"Key\":\"" + model.Key + "\"");
            json.Append("}]");
            return json.ToString();
        }

        private string ObjectToJson(IList<Ue.Qcdz.Domain.TreeViews.TreeNode> list)
        {
            if (list == null) return string.Empty;
            StringBuilder json = new StringBuilder();
            json.Append("[");

            if (list.Count > 0)
            {

                for (int i = 0; i < list.Count; i++)
                {

                    if (json.ToString().Length > 3)
                        json.Append(",");

                    json.Append("{");
                    json.Append("\"Name\":\"" + list[i].Tag.Name + "\",");
                    json.Append("\"Key\":\"" + list[i].Key + "\"");
                    json.Append("}");


                }
            }
            json.Append("]");
            return json.ToString();
        }
        #endregion
    }
}
